package com.glory.gloryquickui.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner

abstract class ViewController<T>(protected val context: Context?) {
    var data: T? = null
        private set
    var view: View? = null
        private set

    fun attachRoot(root: ViewGroup) {
        val resLayoutId = resLayoutId()
        check(resLayoutId > 0) { "Please check your layout id in resLayoutId() method" }
        view = LayoutInflater.from(context).inflate(resLayoutId, root, false)
        root.addView(view)
        onCreatedView(view)
    }

    fun fillData(data: T) {
        this.data = data
        if (this.data != null) {
            onBindView(data)
        }
    }


    /**
     * indicate layout id of this ViewControl
     *
     * @return layout id
     */
    protected abstract fun resLayoutId(): Int

    /**
     * view has been
     *
     * @param view real view
     */
    protected abstract fun onCreatedView(view: View?)

    /**
     * bind data to view
     *
     * @param data data
     */
    protected abstract fun onBindView(data: T)


    open fun masterPart():View?{
        return null
    }
}